
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
import numpy as np
import time

import pandas as pd
 # 载入数据
 
def Load(Path):
    #r"D://python_论文//try//8-18try//right2.csv"
    global a,Y,X_train, X_test, y_train, y_test,X_vali,y_vali, X_train_vali,y_train_vali
    df = pd.read_csv(Path)
#    df=df.drop([0,2,5,9,14,20,27,35,44,54,65,77,90,104,119,135,152,170,189,209],axis=1)
    df=df.sample(frac=1).reset_index(drop=True)
    a=df.values[:, 0:380]
    
    #X = np.expand_dims(a.astype(float), axis=2)
    Y = df.values[:, 380]
    X_train_vali, X_vali,y_train_vali,y_vali = train_test_split(a, Y, test_size = 0.20, random_state = 0)
    X_train, X_test, y_train, y_test = train_test_split(X_train_vali, y_train_vali, test_size = 0.25, random_state = 0)
    return a,Y,X_train, X_test,y_train,y_test,X_vali,y_vali

#X_train_vali, X_test, y_train_vali, y_test = train_test_split(a, Y, test_size = 0.20, random_state = 0)
#X_train, X_vali, y_train, y_vali = train_test_split(X_train_vali, y_train_vali, test_size = 0.25, random_state = 0)

# 划分训练集与测试集
#x_train, x_test, y_train, y_test = train_test_split(a, Y, test_size=0.3, stratify=Y,random_state=0)
# 显示数据
#frame = pd.DataFrame(X_train)
#frame.columns = list(range(0,210))
#print(frame.head())

# 多层感知机,设置隐含层为1层100
def Training(X_train,X_test,y_test, y_train,X_vali,y_vali,k):
        global onedata_list
        onedata_list=[]
        model = MLPClassifier(activation='relu',  alpha=1e-5,hidden_layer_sizes=(k),
                              max_iter=50,batch_size=25,
                              verbose=1,random_state=1)
   
        scaler = StandardScaler()
#        fit = scaler.fit(X_train)
         
        MLPX_train = scaler.fit_transform(X_train)
        MLPX_test = scaler.fit_transform(X_test)
        MLPX_vali = scaler.fit_transform(X_vali)
        # 再次训练并预测
        model.fit(MLPX_train, y_train)
        MLPpredict_yxl = model.predict(MLPX_train)
        MLPpredict_ycs = model.predict(MLPX_test)
        MLPpredict_yyz = model.predict(MLPX_vali)
        MLPscorexl = accuracy_score(y_train, MLPpredict_yxl)
        print(f"训练集中归一化MLP测试的精度为:{MLPscorexl}")
        MLPscorecs = accuracy_score(y_test, MLPpredict_ycs)
        print(f"测试集中归一化MLP测试的精度为:{MLPscorecs}")
        MLPscoreyz = accuracy_score(y_vali, MLPpredict_yyz)
        print(f"验证集中归一化MLP测试的精度为:{MLPscoreyz}")
        onedata_list.append(MLPscorexl)
        onedata_list.append(MLPscorecs)
        onedata_list.append(MLPscoreyz)
        return onedata_list

List_result=[]
for k in [50,100,200,300,400,500,600]:
    Load(r"C:\Users\finally\3.csv")
    Training(X_train,X_test,y_test, y_train,X_vali,y_vali,k)
    List_result.append(onedata_list)

#List=[]
#for i in range(2):
#    List.append(List_result[i][1])
#np.mean(List)

#Train=pd.concat([pd.DataFrame(X_train),pd.DataFrame(y_train)], axis=1)
#Test=pd.concat([pd.DataFrame(X_test),pd.DataFrame(y_test)], axis=1)
#Vali=pd.concat([pd.DataFrame(X_vali),pd.DataFrame(y_vali)], axis=1)
#import matplotlib.pyplot as plt
#L=[]
#L0=[]
#for i in range(20):
#    L.append(List_result[i][1])
#    L0.append(List_result[i][2])
#plt.plot(np.arange(20),L,np.arange(20),L0)
#model['loss']









#Load(r"D://python_论文//try//8-18try//right1.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//foot1.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//left2.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//right2.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//foot2.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//rightfootleft1.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#Load(r"D://python_论文//try//8-18try//rightfootleft2.csv")
#Training(X_train,X_test,X_vali,y_test,y_vali, y_train)
#List_result.append(onedata_list)
#    #




#########################################################################
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=3,weights='distance',algorithm='brute',p=1,verbose=1,)

# 第三步： 训练模型
from sklearn.model_selection import cross_val_score
knn = KNeighborsClassifier(n_neighbors=7)            # 训练模型
knn.fit(X_train, y_train)
print(knn.predict(X_test))            # 利用模型做预测
print(y_test)
print(knn.score(X_test, y_test))      # 模型打分
#from sklearn.cross_validation import cross_val_score # K折交叉验证模块

scores = cross_val_score(knn, a, Y, cv=5, scoring='accuracy')
print(scores.mean())                # 使用K折交叉验证模块


# 第四步：建立测试参数集,调参，优化模型
k_range = range(1, 15)
k_scores = []
k_loss=[]
import matplotlib.pyplot as plt

#藉由迭代的方式来计算不同参数对模型的影响，并返回交叉验证后的平均准确率
for k in k_range:
    knn = KNeighborsClassifier(n_neighbors=k)
    scores = cross_val_score(knn, a, Y, cv=10, scoring='accuracy')     # 分类
#    loss = -cross_val_score(knn, a, Y, cv=10, scoring='mean_squared_error')    # 回归
    k_scores.append(scores.mean())
#    k_loss.append(loss.mean())
#可视化数据


 #设置数字标签**
plt.plot(k_range, k_scores,'-.bo')
plt.grid(axis='y')
for a,b in zip(k_range,k_scores):
    plt.text(a, b+0.001, '%.4f' % b, ha='center', va= 'bottom',fontsize=9)
#plt.plot(k_range, k_loss,'r:d')
plt.xlabel('Value of K for KNN')
plt.ylabel('Cross-Validated Accuracy')
plt.show()

knn = KNeighborsClassifier(n_neighbors=7)
knn.fit(X_train, y_train)
predict_ycs=knn.predict(X_test)
KNNscorecs =  accuracy_score(y_test, predict_ycs)
print(f"KNN测试集的精度为:{KNNscorecs}")
predict_yyz=knn.predict(X_vali)
KNNscoreyz =  accuracy_score(y_vali, predict_yyz)
print(f"KNN验证集的精度为:{KNNscoreyz}")
#
#

